package com.gmrz.uas.plugin.caservice;

import com.gmrz.uas.plugin.authservice.AuthServiceConfigSpec;
import com.gmrz.uas.plugin.authservice.AuthServiceDataSpec;
import com.gmrz.uas.plugin.authservice.exception.InvalidConfigException;
import com.gmrz.uas.plugin.caservice.bean.CertBean;
import com.gmrz.uas.plugin.caservice.bean.CertTransactionTextBean;
import com.gmrz.uas.plugin.caservice.bean.P10Bean;
import com.gmrz.uas.plugin.caservice.bean.P7Bean;
import com.gmrz.uas.plugin.exception.PluginException;
import com.gmrz.uas.plugin.provider.ServiceProvider;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;

/**
 * Created by gongyuqiang on 2017/5/24.
 */
public class BaseCertServicePlugin implements CertServicePlugin {

    protected Map<String, String> allConfig = null;
    protected Map<String, AuthServiceConfigSpec> allConfigSpec = null;
    protected Map<String, AuthServiceDataSpec>  allDataSpec = null;
    protected ServiceProvider uasServiceProvider = null;

    public void close(Connection c)
    {
        try
        {
            if (c != null)
                c.close();
        }
        catch (Exception ex)
        {

        }
    }

    public void close(Statement s)
    {
        try
        {
            if (s != null)
                s.close();
        }
        catch (Exception ex)
        {

        }
    }

    public void close(ResultSet rs)
    {
        try
        {
            if (rs != null)
                rs.close();
        }
        catch (Exception ex)
        {
        }
    }
    @Override
    public String getPluginId() {
        return null;
    }

    @Override
    public void setServiceProvider(ServiceProvider provider) {
        this.uasServiceProvider = provider;
    }

    @Override
    public Map<String, String> getAllConfig() {
        return this.allConfig;
    }

    @Override
    public void setAllConfig(Map<String, String> allConfig) throws InvalidConfigException {
        if(allConfigSpec != null){
            //检测丢失的参数
            for(String key : allConfigSpec.keySet()){
                if(!allConfigSpec.get(key).isOptional() && !allConfig.containsKey(key)){
                    throw new InvalidConfigException("Key[" + key + "] is missing");
                }
            }
        }

        this.allConfig = allConfig;
    }

    @Override
    public CertBean createCert(P10Bean p10Bean) throws PluginException {
        return null;
    }

    @Override
    public boolean verifySignature(P7Bean p7Bean) throws PluginException, UnsupportedEncodingException {
        return false;
    }

    @Override
    public byte[] revokeCert(CertBean certBean, String appId) throws PluginException {
        return null;
    }

//    @Override
//    public byte[] revokeCert(CertBean certBean) throws PluginException {
//        return null;
//    }


    @Override
    public CertTransactionTextBean getCertTransactionText(String transactionText) throws PluginException {
        return null;
    }

    @Override
    public Map<String,String> getDN(String dn) throws PluginException {
        return null;
    }
}
